package com.mes.service.quality;

import com.mes.entity.User;
import com.yy.base.common.dao.Dao;
import com.yy.base.common.service.BaseService;
import com.yy.base.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ProductTestService extends BaseService {
	@Autowired
	private Dao dao;

	//根据RFID带出对应明细信息
	public List<Map<String,Object>> getPhotoLists(String code) {

		return dao.query("SELECT  " +
				"  zy.CODE AS ZYCODE,  " +
				"  wl.  " +
				"VALUE  " +
				"  || ' - ' || WL.NAME PRODUCT,  " +
				"  wl.m_product_id as PRODUCTID,  " +
				"  zy.emeso_job_order_id as ZYID,  " +
				"  ( SELECT a.STATUS FROM Emeso_Job_Order_Exe a WHERE a.route_no = mx.route_no - 10 AND a.emeso_job_order_id = zy.Emeso_Job_Order_id ) AS ZYGXSTATUS,  " +
				"  xm.emesq_inspection_group_id as GROUPID,  " +
				"  gygx.emes_process_id as PROCESSID, " +
				"  item.emesq_inspection_item_id as ITEMID, " +
				"  mx.emeso_job_order_exe_id as EXEID, " +
				"  jh.color AS YSNAME,  " +
				"  to_char(mx.Exe_Date_To, 'yyyy-mm-dd hh24:mi') AS TIMES,  " +
				"  (case when mx.STATUS = 'CREATE' then '待检' else '已检' end )AS MXSTATUS,  " +
				"  mx.STATUS jo_status, " +
				"  item.NAME AS XMNAME   " +
				"FROM  " +
				"  emeso_job_order zy,  " +
				"  emeso_job_order_exe mx,  " +
				"  Emesq_Quality_ROR jy,  " +
				"  Emesq_Inspection_Group_Item xm,  " +
				"  Emesq_Inspection_Item item,  " +
				"  emes_process gygx,  " +
				"  emes_processopr gx,  " +
				"  emeso_manuorder sc,  " +
				"  emesp_planorder jh,  " +
				"  m_product wl   " +
				"WHERE  " +
				"  zy.emeso_job_order_id = mx.emeso_job_order_id   " +
				"  AND wl.m_product_id = zy.m_product_id   " +
				"  AND zy.emeso_manuorder_id = sc.emeso_manuorder_id   " +
				"  AND sc.emesp_planorder_id = jh.emesp_planorder_id   " +
				"  AND mx.emes_process_id = gygx.emes_process_id   " +
				"  AND gygx.emes_processopr_id = gx.emes_processopr_id   " +
				"  AND gx.emes_processopr_id = jy.emes_processopr_id   " +
				"  AND jy.emesq_inspection_group_id = xm.emesq_inspection_group_id   " +
				"  AND xm.emesq_inspection_item_id = item.emesq_inspection_item_id   " +
				"  AND gygx.NAME = '成品检验' " +
                "  AND zy.CODE = '"+code+"'");
	}

	//根据RFID找出信息
	public List<Map<String,Object>> getLists(String code) {

		return dao.query("SELECT " +
				"  zy.code as ZYCODE,  xm.name   " +
				"FROM  " +
				"  emeso_job_order zy,  " +
				"  emeso_job_order_exe exe,  " +
				"  emes_process gx,   " +
				"  Emesq_Quality_ROR gxjy,  " +
				"  emesq_inspection_group_item item ,  " +
				"  emes_color ys,  " +
				"  m_product wl,  " +
				"  Emesq_Inspection_Item xm  " +
				"WHERE  " +
				"  zy.emeso_job_order_id = exe.emeso_job_order_id   " +
				"  and zy.m_product_id = wl.m_product_id  " +
				"  and exe.emes_process_id = gx.emes_process_id  " +
				"  AND gx.emes_processopr_id = gxjy.emes_processopr_id  " +
				"  and gxjy.emesq_inspection_group_id = item.emesq_inspection_group_id  " +
				"  and item.emesq_inspection_item_id = xm.emesq_inspection_item_id  " +
				"  and zy.code = '"+code+"'  " +
				"  and gx.name  = '成品检验'");
	}


	/**
	 * 往质量结果中插入数据
	 * @return
	 */
	public String inQualityResult(String productId, String processId, String status, String results,String jobid, User user, String GroupId) {

		String sql = "insert into Emesq_Quality_Result "
				+ " (Emesq_Quality_Result_ID,M_Product_Id,Emes_Process_Id,Emeso_Job_Order_Id,Status,Result,Emesq_Inspection_Group_Id,"
				+ "AD_CLIENT_ID, AD_ORG_ID,ISACTIVE, CREATED, CREATEDBY, UPDATED, UPDATEDBY)" +
				" values(?,?,?,?,?,?,?,?,?,?,now(),?,now(),?)";


	    String uuid = StringUtil.getUUID();
		dao.executeSql(sql, uuid,productId,processId,jobid,status, results,GroupId, user.getClientId(), user.getOrgId(), "Y", user.getId(), user.getId());

		return uuid;
	}

	/**
	 * 往质量结果中插入数据
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public boolean inQualityResultValue(String resultId, String ITEMID, User user, String name) {
		boolean result = false;
		String uuids = StringUtil.getUUID();

		String sql = "insert into Emesq_Quality_Result_Value "
				+ " (Emesq_Quality_Result_Value_Id,Emesq_Quality_Result_ID,Emesq_Inspection_Item_Id,Name,"
				+ "AD_CLIENT_ID, AD_ORG_ID,ISACTIVE, CREATED, CREATEDBY, UPDATED, UPDATEDBY)" +
				" values(?,?,?,?,?,?,?,now(),?,now(),?)";


		result =  dao.executeSql(sql, uuids,resultId,ITEMID, name, user.getClientId(), user.getOrgId(), "Y", user.getId(), user.getId()) > 0;

		return result;
	}

	//修改工序的执行状态为完工或者不合格
	@SuppressWarnings("unchecked")
	public boolean UpdateExeEndStatus(String Ids ,String status) {
		boolean result = false;


		String sql =  "UPDATE Emeso_Job_Order_Exe SET status = ?, Exe_Date_To =  now()  WHERE  Emeso_Job_Order_Exe_id = ? ";

		result = dao.executeSql(sql, status,Ids) > 0;

		return result;
	}
}